﻿using System;
using System.Text;
using System.Windows.Forms;
using PhiStore.Threading;

namespace PhiStore
{
    public partial class LogViewer : Form
    {
        public LogViewer()
        {
            InitializeComponent();
            timer1.Start();
        }

        private ulong previousBeeps = 0L;
        private void timer1_Tick(object sender, EventArgs e)
        {
            Log l = Log.Instance;
            long count = 0;
           
            StringBuilder sb = new StringBuilder();
            while (l.Count > 0)
            {
                sb.AppendLine(l.ReadLogEntry());
                count++;
            }

            LogBox.Text += sb.ToString();

            if (count > 0)
            {
                LogBox.SelectionStart = LogBox.Text.Length;
                LogBox.ScrollToCaret();
            }

            if (count == 0 && Log.Instance.Beeps() != previousBeeps)
            {
                previousBeeps = Log.Instance.Beeps();
                LogBox.Text += previousBeeps.ToString() + " keep alive messages received";
                LogBox.SelectionStart = LogBox.Text.Length;
                LogBox.ScrollToCaret();
                
            }
        }

        private void LogViewer_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();
        }

        private void LogBox_DoubleClick(object sender, EventArgs e)
        {
            LogBox.Clear();
        }
    }

    public class LogViewerBooster : Boostable
    {
        public void Run()
        {
            LogViewer lv = new LogViewer();
            lv.ShowDialog();
        }
    }
}
